package  org.FaPe3D
{
	import br.com.stimuli.loading.BulkLoader;
	
	public class FaPe3D
	{
		private static var _adapters:Array = new Array();
		
		public function FaPe3D()
		{
		}
		
		public function add(id:String, settingPath:String, commonClass:Class = null):Adapter
		{
			var _commonClass:* = (commonClass  == null ? null :  ((commonClass+'').indexOf('[class') == 0 ? new commonClass() : null) );
			
			var _ap:Adapter = new Adapter(id, settingPath, _commonClass);
			_adapters.push({
							id      : id ,
				            adapter :   _ap ,
				            commonClass : _commonClass
				           });
			
			return _ap;
		}
		
		public static function get adapters():Array
		{
			return _adapters;	
		}
		
		public function getAdapterById(id:String):Adapter
		{
			var ret:Adapter = null;
			for(var i:uint=0; i<_adapters.length; i++)
			{
				if(_adapters[i].id == id)
				{
					// start load & parsing process
					ret = _adapters[i].adapter;	
				}
			}
			
			return ret;
		}

		public function start(id:String):Adapter
		{
			var ap:Adapter = getAdapterById(id);
						
			if(ap !=  null)
			{
				ap.start();
			}
			
			return ap;
		}
		
		public function reStart(id:String):Adapter
		{
			BulkLoader.removeAllLoaders();
			//
			return start(id);
		}
		
		public function remove(id:String):Boolean
		{
			var ret:Boolean = false;
			var temp:Array = new Array();
			//
			for(var i:uint=0; i<_adapters.length; i++)
			{
				if(_adapters[i].id != id)
				{
					temp.push(_adapters[i]);
				}else
				{
					ret = true;
				}
			}
			// replace _adapters
			_adapters = temp;
			
			return ret;
		}
		
		public function removeAll():void
		{	
			for(var i in BulkLoader._allLoaders)
			{
				if( (i+'').indexOf( 'FAPE3D_' ) == 0)
				{
					BulkLoader.getLoader(i).removeAll();
					BulkLoader.getLoader(i).clear();
				}
			}
			//
			_adapters = new Array();
		}
		
			
	}
}